#ifndef _MODULE_JOB
#define _MODULE_JOB
#include "page.module.js"
#include "prefs.module.js"
#include "boss.module.js"
#include "console.module.js"
#include "task.lib.js"
#include "ajax.lib.js"
#include "cache.lib.js"
#include "utility.lib.js"
#include "usolib.lib.js"

Modules.push( "Job" );

var Job = {
	running: false,
	list: false,
	energy: false,
	job_id: false,
	wait: 300,
	count: 0,
	init: function(resume) {
		if (resume) {
			if (Job.running === false) {
				Page.toggle('job');
			}
			return;
		}
		Job.count = 0;
		Job.running = true;
		Job.list = false;
		Job.wait = 300;
		Job.energy = false;
		Job.job_id = false;
		Job.log('Initiating Job Automation');
		Ajax(Page.domain + '/mobwars/profile/', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Job.init(true);
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			if (m = xhr.responseText.match(/>.*?Level [0-9]*? (.*?)<\/h1>/)) {
				if ('insomniac' == m[1].toLowerCase()) Job.wait = 240;
			} else {
				Job.init();
				return;
			};
			if (Settings.insider) Job.wait * 0.9;
			Job.calculate();
			return;
		});
		return;
	},
	log: function(action) {
		var log = [{
			name: 'Script',
			value: 'Job'
		},
		{
			name: 'Action',
			value: action
		}];
		Console.log(log);
		Console.status('job', action);
		return;
	},
	update_list: function(html) {
		if (Job.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Job.check_stats();
				return;
			});
			return;
		};
		Job.list = [];
		var doc = document.createElement('div'),
			tables,
			re_energy = new RegExp('Energy:.*?(\\d+)');
		doc.innerHTML = html;

		tables = $("//div[@id='app8743457343_content']/div/table", doc);
		$("//tr[contains(@class,'rowData')]", tables[0]).each(function() {
			Job.list.push({
				id: parseInt($("//input[@name='jobid']", this)[0].value),
				name: $("//b[contains(@style,'color: silver;')]", this)[0].innerHTML,
				energy: parseInt(this.innerHTML.match(re_energy)[1])
			});
		});

		if (Job.list.length <= 0) return false;
		return true;
	},
	calculate: function() {
		if (Job.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Job.calculate();
				return;
			});
			return;
		};
		if (Job.list === false) {
			Job.log('Loading Job List');
			Ajax(Page.domain + '/mobwars/jobs/', function(xhr) {
				Page.captcha(xhr.responseText);
				if (Page.pausing === true) {
					Page.pause_cb(function() {
						Job.calculate();
						return;
					});
					return;
				};
				Boss.update(xhr.responseText);
				Job.update_list(xhr.responseText);
				Job.calculate();
				return;
			});
			return;
		}
		Job.log('Checking Job List');
		if (c_job = Job.list[Settings.job_number - 1]) {
			if (c_job.energy > Boss.energy[1]) {
				Job.log('Not Enough Total Energy');
				var timer = new Utility.timer({
					time: 5,
					message: 'Not Enough Total Energy [[time]]...',
					script: 'job',
					fn: function() {
						Page.toggle('job');
						return;
					}
				});
				timer.start();
				return;
			}
			Job.job_id = c_job.id;
			Job.energy = c_job.energy;
			Job.check_stats();
		} else {
			Job.log('Can\'t Find Job');
			var timer = new Utility.timer({
				time: 5,
				message: 'Can\'t Find Job [[time]]...',
				script: 'job',
				fn: function() {
					Page.toggle('job');
					return;
				}
			});
			timer.start();
		}
		return;
	},
	check_stats: function() {
		if (Job.running === false) {
			return;
		};
		if (Page.pausing === true) {
			Page.pause_cb(function() {
				Job.check_stats();
				return;
			});
			return;
		};
		Job.log('Checking Boss\'s Stats');
		var updating = false;
		if (Boss.cash >= Settings.bank_limit && Settings.bank && Cache.banking === false) {
			Job.log('Banking ' + Utility.int_dollars(Boss.cash));
			Task.bank(Job.calculate);
			updating = true;
		}
		if (Job.energy > Boss.energy[0]) {
			Job.log('Recharging Boss\'s Energy to ' + Job.energy);
			var timer = new Utility.timer({
				script: 'job',
				message: 'Waiting [[time]] for ' + Job.energy + ' Energy...',
				time: Job.wait * (Job.energy - Boss.energy[0]),
				fn: function() {
					Job.calculate();
					return;
				}
			});
			timer.start();
			updating = true;
		}
		if (updating) {
			Job.list = false;
			return;
		}
		Job.log('Stats Check Complete');
		Job.log( 'Doing Job Number ' + Settings.job_number );
		var timer = new Utility.timer({
			time: Page.wait(),
			message: 'Doing Job Number ' + Settings.job_number + ' [[time]]...',
			script: 'job',
			fn: function() {
				if (Job.running === false) {
					return;
				}
				Job.do_job();
			}
		});
		timer.start();
	},
	do_job: function() {
		Ajax(Page.domain + '/mobwars/jobs/do.php?action=dojob&jobid=' + Job.job_id, function(xhr) {
			Page.captcha(xhr.responseText);
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Job.calculate();
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Job.update_list(xhr.responseText);
			Task.announce(xhr.responseText);
			Job.count++;
			if (Job.count >= Settings.script_repeat && Settings.script_repeat != -1) {
				Page.toggle('job');
				return;
			}
			Job.calculate();
			return;
		},
		'POST');
	}
};
#endif
